package com.gym.code.config.admin.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gym.code.config.admin.bean.db.DBBaseMappingInfo;
import com.gym.code.config.admin.bean.db.DBMappingInfo;
import com.gym.code.config.admin.bean.table.TableMappingBean;
import com.gym.code.config.admin.client.DBHelpService;
import com.gym.code.config.admin.client.model.DBDatabaseModel;
import com.gym.code.config.admin.client.model.DBTableModel;
import com.gym.code.config.admin.service.IDBMappingService;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 */
@Controller
@RequestMapping("/db")
public class DBController {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IDBMappingService dbService;
	
	@Autowired
	private DBHelpService dbHelpService;
	
	@RequestMapping("/list")
	public ModelAndView dbList() {
		List<DBBaseMappingInfo> list = dbService.queryBaseAll();
		return new ModelAndView("db/list","list",list);
	}
	
	
	
	@RequestMapping("/pAdd")
	public ModelAndView pAdd() {
		return new ModelAndView("/db/add");
	}
	
	@RequestMapping("/add")
	public ModelAndView add(
			String ip,
			int port,
			String dbName,
			String username,
			String password,
			String jdbcTemplateName,
			String basePath,
			String packagePath,
			String creater
			) {
		DBBaseMappingInfo bean = new DBBaseMappingInfo();
		bean.setIp(ip);
		bean.setDbName(dbName);
		bean.setPort(port);
		bean.setUsername(username);
		bean.setPassword(password);
		bean.setJdbcTemplateName(jdbcTemplateName);
		bean.setCreater(creater);
		bean.setBasePath(basePath);
		bean.setPackagePath(packagePath);
		dbService.insert(bean);
		return new ModelAndView("redirect:/db/list.do");
	}
	
	
	
	@RequestMapping("/pUpdate")
	public ModelAndView pUpdate(long id){
		DBBaseMappingInfo bean =  dbService.queryBase(id);
		return new ModelAndView("db/update","bean",bean);
	}
	
	@RequestMapping("/update")
	public ModelAndView update(
			long id,
			String ip,
			int port,
			String dbName,
			String username,
			String password,
			String jdbcTemplateName,
			String basePath,
			String packagePath,
			String creater) {
		DBBaseMappingInfo bean = new DBBaseMappingInfo();
		bean.setId(id);
		bean.setIp(ip);
		bean.setDbName(dbName);
		bean.setPort(port);
		bean.setUsername(username);
		bean.setPassword(password);
		bean.setJdbcTemplateName(jdbcTemplateName);
		bean.setCreater(creater);
		bean.setBasePath(basePath);
		bean.setPackagePath(packagePath);
		dbService.update(bean);
		return new ModelAndView("redirect:/db/list.do");
	}
	
	
	@RequestMapping("/config")
	public ModelAndView config(long id) {
		DBMappingInfo bean = dbService.query(id);
		DBDatabaseModel dbModel = wrapperDBTableModel(bean);
		
		List<DBTableModel> modelList = dbHelpService.getTableInfoList(dbModel);
		List<TableMappingBean> tableMappingList = bean.getTableList();
		
		wrapConfirgedInfo(tableMappingList,modelList);
		
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("list", modelList);
		model.put("bean", bean);
		return new ModelAndView("/db/config",model);
	}
	
	
	private void wrapConfirgedInfo(List<TableMappingBean> tableMappingList,List<DBTableModel> modelList) {
		Map<String,TableMappingBean> mappingMap = new HashMap<String,TableMappingBean>();
		for(TableMappingBean item : tableMappingList){
			mappingMap.put(item.getTableName(), item);
		}
		for(DBTableModel item : modelList){
			item.setTableMappingBean(mappingMap.get(item.getTableName()));
		}
	}
	
	public static DBDatabaseModel wrapperDBTableModel(DBBaseMappingInfo db){
		DBDatabaseModel dbModel = new DBDatabaseModel();
		dbModel.setDbName(db.getDbName());
		dbModel.setIp(db.getIp());
		dbModel.setPort(db.getPort());
		dbModel.setUsername(db.getUsername());
		dbModel.setPassword(db.getPassword());
		return dbModel;
		
	}
	
	@RequestMapping("/delete")
	public ModelAndView delete(long id){
		dbService.delete(id);
		return new ModelAndView("redirect:/db/list.do");
	}
	
}
